﻿package
{
	/*
	 * author:young;
	 * one class of a beautiful slider
	 * 
	 * 
	 * */
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	[SWF(width='450',heigth='200',frameRate='50',backgroundColor='0xffffff')]
	public class FScroll extends Sprite
	{
		private var barSize:int;
		private var barStrength:int;
		private var m:Matrix;
		private var spriteBar:Sprite;
		private var spriteKnob:Sprite;
		private var dx:Number;
		private var startX:Number;
		public function FScroll(barSize:int=250,barStrength:int=20):void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			this.barSize = barSize;
			this.barStrength = barStrength;
			m = new Matrix();
			createBar();
			createKnob();
			//redrawKnob();
			spriteKnob.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		private function createBar():void
		{
			spriteBar = new Sprite();
			m.createGradientBox(barSize, barStrength, Math.PI/2, 0, 0);
			spriteBar.graphics.beginGradientFill(GradientType.LINEAR, [0xcccccc, 0xffffff, 0xdddddd], [1, 1, 1], [0, 240, 250], m);
			spriteBar.graphics.drawRoundRect(0, 0, barSize, barStrength, barStrength,barStrength);
			spriteBar.graphics.endFill();
			spriteBar.filters = [new DropShadowFilter(0, 0, 0x999999, 1, 8, 8, 1, 3, true),new DropShadowFilter(0,0,0x333333,1,2,2,1,3,true)];
			spriteBar.cacheAsBitmap = true;
			addChild(spriteBar);
			spriteBar.y = stage.stageHeight / 2;
			startX=spriteBar.x = 10;
		}
		
		private function createKnob():void
		{
			spriteKnob = new Sprite();
			spriteKnob.buttonMode = true;
			spriteKnob.useHandCursor = true;
			spriteKnob.blendMode = BlendMode.HARDLIGHT;
			spriteKnob.filters = [new DropShadowFilter(0,0,0x0000ff,1,8,8,1,3,true),new DropShadowFilter(0,0,0x000000,1,2,2,1,3,true)];
			spriteKnob.cacheAsBitmap = true;
			addChild(spriteKnob);
			redrawKnob();
		}
		private function redrawKnob():void
		{
			m.createGradientBox(barSize - 2, barStrength - 2, -Math.PI / 2, 1, 1);
			var g:Graphics = spriteKnob.graphics;
			g.beginGradientFill(GradientType.LINEAR, [0x8f88fa, 0x0046b2, 0xffffff], [1, 1, 1], [0, 200, 255], m);
			g.drawRoundRect(1, 1, barSize / 2, barStrength - 1, barStrength - 1, barStrength - 1);
			g.endFill();
			spriteKnob.y = spriteBar.y;
			spriteKnob.x = startX;
		}
		private function onMouseDown(e:MouseEvent):void
		{
			dx = mouseX - spriteKnob.x;
			addEventListener(Event.ENTER_FRAME, onEnter);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		private function onEnter(e:Event):void
		{
			
			spriteKnob.x = mouseX - dx;
			if (spriteKnob.x <= startX)
			{
			spriteKnob.x = startX;
			}
			if (spriteKnob.x >= (spriteBar.width - spriteKnob.width - 2 + startX))
			{
			spriteKnob.x = spriteBar.width - spriteKnob.width - 2 + startX;
			}
		}
		private function onMouseUp(e:MouseEvent):void
		{
			removeEventListener(Event.ENTER_FRAME, onEnter);
		}
	}
}